﻿using Ledict.ConditionExtract.BLL;
using Ledict.ConditionExtract.Common;
using Ledict.ConditionExtract.Model;
using Ledict.Core.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ledict.ConditionExtract
{
    public partial class UpdateDataForm : Form
    {
        readonly NewIDBLL newid = new NewIDBLL();
        readonly CaseCauseBLL casecausebLL = new CaseCauseBLL();
        public UpdateDataForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            StringBuilder sb1 = new StringBuilder();
            try
            {
                string sql = string.Empty;
                sql = " select * from SYS_CaseCause where CC_NodeType=28 ";
                //sql = " select * from SYS_CaseCause where CC_NodeType=2 ";
                //sql = " select * from SYS_CaseCause where CC_NodeType=2 and cc_ID not in('1182','1655','1147','1181','1688','1670','1180','1135','1183','1619','2199','1658','1145','1534','1743','1186','1189','2498','1045','1758','2155','1290','1621','1611','1615','1770','1854','1939','1940','1945','1640','1641','1642','1643','1659','1537','1538','1646','1520','1514') ";
                Database oBase = DatabaseFactory.CreateDatabase("JudgeDoc");
                DbCommand oCmd = oBase.GetSqlStringCommand(sql);
                DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
                if (OtB.Rows.Count > 0)
                {
                    foreach (DataRow dr in OtB.Rows)
                    {
                        string ccid = dr["CC_ID"].ToString();
                        string allParentName = dr["AllParentName"].ToString();
                        string allParentId = dr["AllParentId"].ToString();
                        string ccName = dr["CC_Name"].ToString();
                        //判断该案由下是否包含这些要素
                        sql = $" select *from SYS_CaseCause where CC_ParentID='{ccid}' and (CC_Name like'%诉请要素%' or CC_Name like'%事实要素%' or CC_Name like'%基本要素%'or CC_Name like'%结果要素%'or CC_Name like'%理由要素%'or CC_Name like'%争议焦点要素%') ";
                        oCmd = oBase.GetSqlStringCommand(sql);
                        DataTable OtBNew = oBase.ExecuteDataSet(oCmd).Tables[0];
                        if (OtBNew != null && OtBNew.Rows.Count > 0)
                        {
                            Log.WriteLog("开始处理案由节点：" + ccid);
                            txbShow.Text += "开始处理案由节点：" + ccid+"\r\n";
                            //有数据则先添加案由要素节点
                            var model = new SYS_CaseCause();
                            model.CC_ID = newid.JudgeDoc_CreateID("SYS_CaseCause");
                            model.CC_Name = "案由要素";
                            model.CC_ParentID = ccid;
                            model.CC_NodeType = 11;
                            model.PIS_ShowName = "案由要素";
                            model.AllParentId = dr["AllParentId"].ToString() + "," + model.CC_ID;
                            model.AllParentName = dr["AllParentName"].ToString() + "," + model.CC_Name;
                            model.HW_level = model.AllParentName.Split(',').Length;
                            model.CC_IsProcessNode = 0;
                            model.CC_IsCombination = 0;
                            model.CC_CommonCase = 0;
                            model.CC_SerialNumb = 0;
                            model.XmlPath = dr["XmlPath"].ToString() + "/" + model.CC_Name;
                            string ccIdNew = casecausebLL.Exists(model.CC_Name, model.CC_ParentID);
                            if (!string.IsNullOrEmpty(ccIdNew))
                            { 
                                string dd = dr["AllParentId"].ToString() + "," + ccIdNew;
                                foreach (DataRow dr1 in OtBNew.Rows)
                                {
                                    string ccId1 = dr1["CC_ID"].ToString();
                                    var mode = casecausebLL.GetCaseCauseByCCID(ccId1);
                                    mode.CC_ParentID = ccIdNew;
                                    string allparentN = mode.AllParentName.Replace(allParentName, model.AllParentName);
                                    string allparentI = mode.AllParentId.Replace(allParentId, dd);
                                    string xmlP = mode.XmlPath.Replace("/" + ccName, "/案由要素");
                                    //先修改子级节点
                                    DataTable dtAll = casecausebLL.GetAllChild(ccId1);
                                    if (dtAll != null && dtAll.Rows.Count > 0)
                                    {
                                        foreach (DataRow dtAr in dtAll.Rows)
                                        {
                                            var mode1 = casecausebLL.GetCaseCauseByCCID(dtAr["CC_ID"].ToString());
                                            mode1.AllParentName = mode1.AllParentName.Replace(mode.AllParentName, allparentN);
                                            mode1.AllParentId = mode1.AllParentId.Replace(mode.AllParentId, allparentI);
                                            mode1.XmlPath = mode1.XmlPath.Replace(mode.XmlPath, xmlP);
                                            casecausebLL.UpdateSYS_CaseCause(mode1, "syb");
                                            //txbShow.Text += "处理节点：" + mode1.CC_ID + "\r\n";
                                        }
                                    }
                                    //然后修改父级节点
                                    mode.AllParentName = allparentN;
                                    mode.AllParentId = allparentI;
                                    mode.XmlPath = xmlP;
                                    casecausebLL.UpdateSYS_CaseCause(mode, "syb");
                                }
                                Log.WriteLog("案由节点处理完成：" + ccid);
                                txbShow.Text += "案由节点处理完成：" + ccid + "\r\n";
                            }
                            else
                            {
                                if (casecausebLL.AddSYS_CaseCause(model, "syb"))
                                {

                                    foreach (DataRow dr1 in OtBNew.Rows)
                                    {
                                        string ccId1 = dr1["CC_ID"].ToString();
                                        var mode = casecausebLL.GetCaseCauseByCCID(ccId1);
                                        mode.CC_ParentID = model.CC_ID;
                                        string allparentN = mode.AllParentName.Replace(allParentName, model.AllParentName);
                                        string allparentI = mode.AllParentId.Replace(allParentId, model.AllParentId);
                                        string xmlP = mode.XmlPath.Replace("/" + ccName, "/案由要素");
                                        //先修改子级节点
                                        DataTable dtAll = casecausebLL.GetAllChild(ccId1);
                                        if (dtAll != null && dtAll.Rows.Count > 0)
                                        {
                                            foreach (DataRow dtAr in dtAll.Rows)
                                            {
                                                var mode1 = casecausebLL.GetCaseCauseByCCID(dtAr["CC_ID"].ToString());
                                                mode1.AllParentName = mode1.AllParentName.Replace(mode.AllParentName, allparentN);
                                                mode1.AllParentId = mode1.AllParentId.Replace(mode.AllParentId, allparentI);
                                                mode1.XmlPath = mode1.XmlPath.Replace(mode.XmlPath, xmlP);
                                                casecausebLL.UpdateSYS_CaseCause(mode1, "syb");
                                                txbShow.Text += "处理节点：" + mode1.CC_ID + "\r\n";
                                            }
                                        }
                                        //然后修改父级节点
                                        mode.AllParentName = allparentN;
                                        mode.AllParentId = allparentI;
                                        mode.XmlPath = xmlP;
                                        casecausebLL.UpdateSYS_CaseCause(mode, "syb");
                                    }
                                    Log.WriteLog("案由节点处理完成：" + ccid);
                                    txbShow.Text += "案由节点处理完成：" + ccid + "\r\n";
                                    sb1.Append("'" + ccid + "',");
                                }
                            }
                        }
                    }
                    System.Windows.Forms.MessageBox.Show("节点处理完成！");
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("处理数据出错：" + ex.Message+sb1.ToString());
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            StringBuilder sb1 = new StringBuilder();
            try
            {
                string sql = string.Empty;

                sql = "select *from SYS_CaseCause where CC_Name='案由要素' ";
                //sql = " select * from SYS_CaseCause where CC_NodeType=2 and cc_ID not in('1182','1655','1147','1181','1688','1670','1180','1135','1183','1619','2199','1658','1145','1534','1743','1186','1189','2498','1045','1758','2155','1290','1621','1611','1615','1770','1854','1939','1940','1945','1640','1641','1642','1643','1659','1537','1538','1646','1520','1514') ";
                Database oBase = DatabaseFactory.CreateDatabase("JudgeDoc");
                DbCommand oCmd = oBase.GetSqlStringCommand(sql);
                DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
                if (OtB.Rows.Count > 0)
                {
                    foreach (DataRow dr in OtB.Rows)
                    {
                        string ccid = dr["CC_ID"].ToString();
                        string allParentName = dr["AllParentName"].ToString();
                        string allParentId = dr["AllParentId"].ToString();
                        string ccName = dr["CC_Name"].ToString();

                        var model = new SYS_CaseCause();
                        model.CC_ID = newid.JudgeDoc_CreateID("SYS_CaseCause");
                        model.CC_Name = "属性";
                        model.CC_ParentID = ccid;
                        model.CC_NodeType = 12;
                        model.AllParentId = dr["AllParentId"].ToString() + "," + model.CC_ID;
                        model.AllParentName = dr["AllParentName"].ToString() + "," + model.CC_Name;
                        model.HW_level = model.AllParentName.Split(',').Length;
                        model.CC_IsProcessNode = 0;
                        model.CC_IsCombination = 0;
                        model.CC_CommonCase = 0;
                        model.CC_SerialNumb = 0;
                        model.XmlPath = dr["XmlPath"].ToString();
                        if (casecausebLL.AddSYS_CaseCause(model, "syb"))
                        {
                            var model1 = new SYS_CaseCause();
                            model1.CC_ID = newid.JudgeDoc_CreateID("SYS_CaseCause");
                            model1.CC_Name = "案由名称";
                            model1.CC_ParentID = model.CC_ID;
                            model1.CC_NodeType = 12;
                            model1.AllParentId = model.AllParentId + "," + model1.CC_ID;
                            model1.AllParentName = model.AllParentName + "," + model1.CC_Name;
                            model1.HW_level = model1.AllParentName.Split(',').Length;
                            model1.CC_IsProcessNode = 0;
                            model1.CC_IsCombination = 0;
                            model1.CC_CommonCase = 0;
                            model1.CC_SerialNumb = 0;
                            model1.XmlPath = model.XmlPath + "/@案由名称";

                            var model2 = new SYS_CaseCause();
                            model2.CC_ID = newid.JudgeDoc_CreateID("SYS_CaseCause");
                            model2.CC_Name = "案由ID";
                            model2.CC_ParentID = model.CC_ID;
                            model2.CC_NodeType = 12;
                            model2.AllParentId = model.AllParentId + "," + model2.CC_ID;
                            model2.AllParentName = model.AllParentName + "," + model2.CC_Name;
                            model2.HW_level = model2.AllParentName.Split(',').Length;
                            model2.CC_IsProcessNode = 0;
                            model2.CC_IsCombination = 0;
                            model2.CC_CommonCase = 0;
                            model2.CC_SerialNumb = 1;
                            model2.XmlPath = model.XmlPath + "/@案由ID";

                            casecausebLL.AddSYS_CaseCause(model1, "syb");
                            casecausebLL.AddSYS_CaseCause(model2, "syb");
                        }                          
                    }
                    System.Windows.Forms.MessageBox.Show("节点处理完成！");
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("处理数据出错：" + ex.Message + sb1.ToString());
            }
           
        }
    }
}
